package com.elinkway.infinitemovies.async;

import android.content.Context;
import android.text.TextUtils;

import com.elinkway.infinitemovies.bean.CommonBean;
import com.elinkway.infinitemovies.dao.PreferencesManager;
import com.elinkway.infinitemovies.utils.LogUtils;
import com.elinkway.infinitemovies.utils.Utils;
import com.lvideo.http.bean.LVideoDataHull;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

/**
 * Created by zhaopeipei on 2016/3/21.
 */
public class RequestPublicIpTask  extends MoviesHttpAsyncTask<CommonBean> {

    Context mContext;

    public RequestPublicIpTask(Context context) {
        super(context);
        mContext = context;
    }

    @Override
    public void netNull() {
        super.netNull();
    }

    @Override
    public void netErr(int updateId, String errMsg) {
        super.netErr(updateId, errMsg);
    }

    @Override
    public void dataNull(int updateId, String errMsg) {
        super.dataNull(updateId, errMsg);
    }

    @Override
    public LVideoDataHull<CommonBean> doInBackground() {
        try {
            URL url = new URL("http://1212.ip138.com/ic.asp");
            BufferedReader br = new BufferedReader(new InputStreamReader(url
                    .openStream()));
            String s = "";
            StringBuffer sb = new StringBuffer("");
            String webContent = "";
            while ((s = br.readLine()) != null) {
                sb.append(s + "rn");
            }


            br.close();
            webContent = sb.toString();
            int start = webContent.indexOf("[") + 1;
            int end = webContent.indexOf("]");
            LogUtils.e("", "webContent=" + webContent);
            LogUtils.e("", "start=" + start);
            LogUtils.e("", "end=" + end);
            webContent = webContent.substring(start, end);
            LogUtils.e("", "webContent=" + webContent);
            if (!TextUtils.isEmpty(webContent)) {
                Utils.setPublicIpAddress(webContent);
                PreferencesManager.getInstance().savePublicIp(webContent);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void onPostExecute(int updateId, CommonBean result) {
    }


}
